/**
 * 
 */
package com.neal.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.thymeleaf.TemplateEngine;

import com.neal.domain.Book;
import com.neal.domain.securities.Function;
import com.neal.domain.securities.Role;
import com.neal.domain.securities.User;


/**
 * @author Neal
 * @version 1.0
 * @description 
 */
@Controller
@RequestMapping("/tl")
@SessionAttributes("user")
public class ThymeLeafController {

	@DateTimeFormat(pattern="yyyy/MM/dd")
	private Date nowDate = new Date();
	
	@Resource(name="templateEngine")
	private TemplateEngine templateEngine;
	
	@RequestMapping("do2")
	public String do2(Model model) {
		// add simple attribute
		model.addAttribute("site", "mySite");
		
		// add Object List
		List<User> users = new ArrayList<User>();
		User u1 = new User();
		u1.setUserName("u1");
		u1.setPassword("pp");
		
		User u2 = new User();
		u2.setUserName("u2");
		users.add(u1);users.add(u2);
		model.addAttribute("users", users);
		
		//add User
		model.addAttribute("user", u1);
		
		//add today
		
		model.addAttribute("today",new Date());
		
		model.addAttribute("book", new Book("1", "2", "3", new GregorianCalendar(2007, 11, 25).getTime()));
		
		//add boolean
		model.addAttribute("isAdmin", true);
		
		//${templateName} :: ${fragName}
		model.addAttribute("fragName", "footer");
		model.addAttribute("templateName", "/tl/footer");
		
		return "tl/home2";
	}
	
	@RequestMapping("do3")
	public String do3(@ModelAttribute("role") Role role, Model model) {
		
		Role r1 = new Role();	r1.setId("r1");	r1.setStatus(false);
		r1.getBoolList().add(true);
		r1.getBoolList().add(false);
		r1.getBoolList().add(true);
		
		Function f1 = new Function(); f1.setEnable(false); f1.setCode("1123123123123");
		Function f2 = new Function(); f2.setEnable(true); f2.setCode("2222222222222");
		Function f3 = new Function(); f3.setEnable(false); f3.setCode("3333333333");
//		r1.getFunctions().add(f1);
//		r1.getFunctions().add(f2);
//		r1.getFunctions().add(f3);
		
		Role r2 = new Role();	r2.setId("r2");
		Role r3 = new Role();	r3.setId("r3");
		
		List<Boolean> boolList = new ArrayList<Boolean>();
		boolList.add(true);
		boolList.add(false);
		
		model.addAttribute("role", r1);
		model.addAttribute("boolList",boolList);
		model.addAttribute("testStatus",false);
		return "tl/home3";
	}
	
	@RequestMapping("do4")
	public String do4(@Valid @ModelAttribute Role role, Model model) {
		return "tl/home3";
		
	}
	
	@RequestMapping("do5")
	public String do5(Model model) {
		return "do5";
	}
	
	@RequestMapping("do6")
	public String do6(Model model) {
		return "do6";
	}
}
